package com.sentinel.demo03.controller;

import com.alibaba.csp.sentinel.Entry;
import com.alibaba.csp.sentinel.SphU;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.sentinel.demo03.handler.MyBlockHandler;
import com.sentinel.demo03.handler.MyFallback;
import com.sentinel.demo03.service.HelloService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
public class HelloController {
    @Autowired
    private HelloService helloService;
    @GetMapping("/hello")
    //注解定义资源 主要使用它是了解 注解属性
    @SentinelResource(
            value="sayHi",
            blockHandler ="sayHiError",
            blockHandlerClass = MyBlockHandler.class,
            fallback="sayHiException",
            fallbackClass = MyFallback.class)
    public String sayHi(String name){
        log.info("luban demo **功能入参:{}",name);
        log.info("业务层注入的对象类型:{}",helloService.getClass().getName());
        String result=null;
        result=helloService.sayHi(name);//控制层这行代码看成保护的资源
        log.info("luban demo **功能入参:{},出参:{}",name,result);
        return result;
    }
    /*public String sayHiException(String name,Throwable e){
        return "对不起 数据彻底错乱了"+e;
    }*/
    //单独使用blockHandler的降级方法
    /*public String sayHiError(String name,BlockException e){
        return "sorry "+name+" 出现问题了"+e;
    }*/
}
